<!DOCTYPE html>
<html lang="zh">
<head>
    <th:block th:include="libs :: header('代码生成模块配置')"></th:block>
</head>
<body>
  <div id="ModulePage">
        <ivz-edit-view ref="vbt" :metas="metas" :config="config"
                         :search-metas="searchMetas" placement="top" height="400">
            <template #name_t="params">
                <a @click="jumpField(params)">{{params.value}}</a>
            </template>
            <template #status_t="params">
                <a-tag v-if="params.value == '已生成'" color="#87d068">{{params.value}}</a-tag>
                <a-tag v-else color="#f50">{{params.value}}</a-tag>
            </template>
        </ivz-edit-view>
      <ivz-modal-form :width="720" title="代码生成配置" :metas="sure.metas" ref="imf"
            :save-meta="sure.saveMeta" :config="sure.config" class="ivz-gen-modal">
          <template #mapper_f><label>{{genPathRoot}}</label></template>
          <template #package_f><label>{{packagePathRoot}}</label></template>
      </ivz-modal-form>
  </div>
</body>
<th:block th:include="libs :: footer"></th:block>
<script>
    let defaultConfig = {
        write: true,
        alias: 'core',
        project: 'admin', // 要生成的代码所在的项目名称
        overwrite: false,
        modulePath: ['backstage', 'admin'], // 要生成项目的父项目
        mavenPath: '\\src\\main\\(java | resources)'
    };
    let ViewType = [
        {label: 'BasicView', value: 'ivz-basic-view'},
        {label: 'EditView', value: 'ivz-edit-view'},
        {label: 'DrawerView', value: 'ivz-drawer-view'}
    ];
    let modules = [
        {label: 'admin', value: 'admin', alias: 'core'},
        {label: 'develop', value: 'develop', alias: 'develop'},
    ];
    let parents = [
        {label: 'backstage', value: 'backstage', children: [
                {label: 'admin', value: 'admin', alias: 'core'},
                {label: 'develop', value: 'develop', alias: 'develop'},
            ]},
        {label: 'plugins', value: 'plugins', children: [
                {label: 'quartz', value: 'quartz', alias: 'quartz'},
            ]},
    ];
    let GenStatus = [
        {label: '已生成', value: '已生成'},
        {label: '未生成', value: '未生成'},
    ];
    let WriteType = [
        {label: 'xml', value: 'XML'},
        {label: 'h5', value: 'OTHER'},
        {label: 'entity', value: 'ENTITY'},
        {label: 'mapper', value: 'MAPPER'},
        {label: 'service', value: 'SERVICE'},
        {label: 'controller', value: 'CONTROLLER'},
        {label: 'serviceImpl', value: 'SERVICE_IMPL'},
    ];
  let vue = new Vue({
      el: "#ModulePage",
      data: {
          metas: [
              {field: 'basic', title: '表基础信息', metas: [
                      {field: 'name', title: '表名', width: 160, fixed: 'left'
                          , disabled: true, sorter: function (a, b) {
                              return a.name.localeCompare(b.name);
                          }},
                      {field: 'prefix', title: 'Join前缀', width: 72},
                      {field: 'msn', title: '所属模块', editable: true, type: 'select'
                          , dictType: 'sys.module', width: 120},
                  ]},
              {field: 'code', title: '要生成的代码信息', metas: [
                      {field: 'entityName', title: '实体名', width: 140, editable: true},
                      {field: 'viewType', title: '视图类型', width: 130, type: 'select', data: ViewType, editable: true},
                      {field: 'disabled', title: '禁用', type: 'switch', width: 56, editable: true
                          , config: {checkedChildren: '是', unCheckedChildren: '否'}},
                      {field: 'status', title: '生成状态', type: 'radio', data: GenStatus, width: 80},
                  ]},
              {field: 'remark', title: '表信息备注', width: -1},
              {field: 'action', title: '操作', type: 'action', width: 280, fixed: 'right'}
          ],
          sure: {
              metas: [
                  {field: 'write', title: '写入项目', type: 'switch', default: 'true', event: {
                            change: function (val, model) {
                                vue.genPathRootHandle(model)
                            }
                      }},
                  {field: 'overwrite', title: '覆写文件', type: 'switch', default: defaultConfig.overwrite},
                  {field: 'modulePath', title: '所属模块', type: 'cascade', data: parents
                      , default: defaultConfig.modulePath, config: {extra: '声明文件要生成到那个模块'}, event: {
                          change: function (val, model) {
                              let $ref = vue.$refs['imf'];
                              let meta = $ref.getMeta("modulePath");
                              let key = "";
                              if(val[1]) {
                                  key = val[1];
                              } else if(val[0]){
                                  key = val[0]
                              } else {
                                  return vue.$msg.warningNotify("未选择模块")
                              }

                              let obj = meta.DataMap[key];
                              if(obj['alias']) {
                                  model['alias'] = obj['alias']
                              } else {
                                  model['alias'] = key;
                              }

                              vue.genPathRootHandle(model);
                          }
                      }},
                  {field: 'alias', title: '模块别名', default: defaultConfig.alias, event: {
                          change: function (val, model) {
                              vue.genPathRootHandle(model)
                          }
                      }, config: {extra: '别名用于包名、url、权限等'}},
                  {field: 'menuId', title: '父菜单', type: 'stree', placeholder: '不选则将不导入数据库'
                      , url: '/core/menu/parent', config: {}, clear: true},
                  {field: 'writeType', title: '类型', type: 'select', clear: true, data: WriteType
                      , placeholder: '用来指定要生成的类型代码, 不选代表生成所有类型代码'
                      , config: {mode: 'tags', maxTagCount: 6}, span: [3, 20, 24]
                  },
                  {field: 'package', title: '包路径', span: [4, 20, 24], config: {extra: ''}},
                  {field: 'mapper', title: '文件路径', span: [4, 20, 24], config: {extra: '注: 写入项目的文件路径=根目录+所属模块+包路径'}}
              ],
              saveMeta: {
                  id: 'save', url: '/dev/table/gen', callBack: function (model) {
                      return new Promise(function () {
                          if(!model['parent'])
                              return vue.$msg.warningNotify("请选择所属模块");

                          let queryString = vue.$qs.stringify(model);
                          let url = '/dev/table/gen?'+queryString;

                          vue.$http.post(url, vue.current).then(function (resp) {
                              vue.$msg.successNotify("生成代码操作", resp['IvzMsg']);
                              vue.$refs['imf'].trigger(); // 关掉弹框
                              vue.$page.query(); // 重新刷新数据
                          }).catch(function (reason) {
                              vue.$msg.errorNotify("生成代码操作", reason);
                          })
                      })
                  }
              },
              config: {
                  form: {
                      column: 2
                  },
                  bodyStyle: {padding: '15px 8px 3px 3px', height: '360px'}
              }
          },
          searchMetas: [
              {field: 'status', title: '生成状态', type: 'select', data: GenStatus, clear: true},
              {field: 'name', title: '表名', clear: true}
          ],
          current: null,
          genPathRoot: "",
          packagePathRoot: '',
          genConfig: null,
          config: {
              form: {
                  column: 4
              },
              table: {
                  submitType: 'entity', // 提交整个实体对象
                  selection: {fixed: 'left'},
                  pagination: {}
              }
          },
      },
      created: function() {
          // 重新修改导入表功能的回调, 添加操作提醒
          this.$page.setActionMeta("import", {callBack: function (row) {
              return new Promise((resolve) => {
                  resolve({tipTitle: '确定要重新导入表配置('+row.name+")"
                      , tipContent: '此动作将会删除原来的表、表字段配置, 生成状态'
                      , success: function (resp) {
                            vue.$page.query(); // 重新导入成功后的回调, 重新加载页面数据
                      }});
              })
          }});
          this.$page.setActionMeta("Del", {callBack: function (row) {
                    return new Promise(function (resolve, reject) {
                        resolve();
                    })
              }});
          // 修改生成代码的回调功能, 未生成状态的直接生成, 已经生成的提醒操作用户
          this.$page.setActionMeta("gen", {callBack: function (row) {
              vue.current = row;
              return new Promise(resolve => {
                  vue.$refs['imf'].open();
              })
          }});
          this.$http.get("/dev/table/config").then(function (resp) {
              vue.genConfig = resp.data;
              vue.genPathRootHandle(defaultConfig)
          })
      },
      methods: {
          jumpField: function (params) {
              let row = params.row;
              // 打开字段配置页面, 并且重新刷新
              this.$nav({path: '/dev/field', params: {tableName: params.value, msn: row.msn}, refresh: true});
          },
          genPathRootHandle(model) {
              let modulePath = model['modulePath'];
              model['parent'] = modulePath[0];
              model['project'] = modulePath[1];

              let parent = modulePath[0] ? modulePath[0] : '\\'
              let project = modulePath[1] ? modulePath[1] : null;

              if(model['write']) { // 使用根项目路径
                  this.genPathRoot = this.genConfig.rootPath+"\\"+parent+(project ? ("\\"+project) : "")+defaultConfig.mavenPath;
              } else {
                  this.genPathRoot = this.genConfig.genPath + "\\" + project + defaultConfig.mavenPath;
              }
              this.packagePathRoot = this.genConfig.parent + "." + model['alias']
                  + ".*.*.java, mapper/xml/*.xml, templates/*.html, /" + model['alias']+"/*(url)"
          }
      }
  });
</script>
</html>
